package spzx.product.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.spzx.model.dto.h5.ProductSkuDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import spzx.product.mapper.ProductMapper;
import spzx.product.service.ProductService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductSku> findProductSkuList() {
        //查询首页上的sku列表
        return productMapper.getProductSkuList();
    }

    @Override
    public PageInfo findByPage(Integer pageNum, Integer pageSize, ProductSkuDto productSkuDto) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSku> list = productMapper.findSkuList(productSkuDto);
        return new PageInfo(list);
    }

    @Override
    public ProductItemVo item(Long skuId) {
        ProductItemVo productItemVo = new ProductItemVo();
        //1.productSku根据skuId查询商品对象
        ProductSku productSku = productMapper.getProductSkuBySkuId(skuId);
        //2.product当前sku对应的spu对象
        Product product = productMapper.getProductById(productSku.getProductId());
        //3.轮播图列表
        String sliderUrls = product.getSliderUrls();
        List<String> sliderUrlList = null;
        if(!StringUtils.isEmpty(sliderUrls)){
            String[] split = sliderUrls.split(",");
            sliderUrlList = Arrays.asList(split);
        }

        //4.detailsImageUrlList详情图列表
        ProductDetails productDetails = productMapper.getProductDetailsByProductId(product.getId());

        String imageUrls = productDetails.getImageUrls();
        List<String> detailsImageUrlList = new ArrayList<>();

        if(!StringUtils.isEmpty(imageUrls)){
            String[] split = imageUrls.split(",");
            detailsImageUrlList = Arrays.asList(split);
        }
        //5.JSONArray specValueList规格值列表
        String specValue = product.getSpecValue();//数据库中，此字段是json数组格式的字符串
        JSONArray specValueList = JSONArray.parseArray(specValue);
        //6.skuSpecValueMap 将当前spu对应的多个sku中规格组合 + skuId 形成一组k-v存储于map中
        List<ProductSku> skuList = productMapper.getProductSkuListByProductId(product.getId());
        Map<String,Object> skuSpecValueMap = skuList.stream().collect(Collectors.toMap(ProductSku::getSkuSpec, ProductSku::getId));

        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        productItemVo.setSliderUrlList(sliderUrlList);
        productItemVo.setDetailsImageUrlList(detailsImageUrlList);
        productItemVo.setSpecValueList(specValueList);

        return productItemVo;
    }
}
